package com.usian.wemedia.controller;

import com.usian.apis.wemedia.WmNewsControllerApi;
import com.usian.model.admin.dtos.NewsAuthDto;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.media.dtos.WmNewsDto;
import com.usian.model.media.dtos.WmNewsPageReqDto;
import com.usian.model.media.dtos.WmNewsVo;
import com.usian.model.media.pojos.WmNews;
import com.usian.wemedia.service.WmNewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.ws.rs.POST;
import java.util.List;

@RestController
@RequestMapping("/api/v1/news")
public class WmNewsController implements WmNewsControllerApi {

    @Autowired
    private WmNewsService wmNewsService;

    @Override
    @PostMapping("/list")
    public ResponseResult findAll(@RequestBody WmNewsPageReqDto wmNewsPageReqDto) {
        return wmNewsService.findAll(wmNewsPageReqDto);
    }

    @Override
    @PostMapping("/submit")/*发布文章*/
    public ResponseResult submitNews(@RequestBody WmNewsDto wmNews) {
        if (wmNews.getStatus().equals(WmNews.Status.SUBMIT.getCode())){
            return wmNewsService.saveNews(wmNews,(short)1);
        }else {
            return wmNewsService.saveNews(wmNews,(short)0);
        }
    }

    @Override
    @GetMapping("/one/{id}")/*回显*/
    public ResponseResult findWmNewsById(@PathVariable Integer id) {
        return wmNewsService.findWmNewsById(id);
    }

    @Override
    @GetMapping("/del_news/{id}")/*删除  先删除中间表 在删除文章表*/
    public ResponseResult delNews(@PathVariable Integer id) {
        return wmNewsService.delNews(id);
    }

    @Override
    @PostMapping("/down_or_up")/*上下架*/
    public ResponseResult downOrUp(@RequestBody WmNewsDto dto) {
        return wmNewsService.downOrUp(dto);
    }

    @Override
    @GetMapping("/findOne/{id}")/*admin端审核 根据id查询自媒体文章*/
    public WmNews findNewsById(@PathVariable Integer id) {
        return wmNewsService.getById(id);
    }

    @Override
    @PostMapping("/update")/* 修改*/
    public ResponseResult updateWmNews(@RequestBody WmNews wmNews) {
        boolean result = wmNewsService.updateById(wmNews);
        if (result){
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
    }

    @Override
    @PostMapping("/findList")/*查询人工审核的文章列表*/
    public PageResponseResult findList(@RequestBody NewsAuthDto dto) {
        return wmNewsService.findListAndPage(dto);
    }

    @Override
    @GetMapping("/find_news_vo/{id}")/*查询人工审核的文章列表详情*/
    public WmNewsVo findWmNewsVo(@PathVariable Integer id) {
        return wmNewsService.findWmNewsVo(id);
    }

    @Override
    @GetMapping("/findRelease")/*查询需要发布的文章id列表*/
    public List<Integer> findRelease() {
        return wmNewsService.findRelease();
    }


}
